package com.javaGame.frame;

import com.javaGame.GameMap;
import com.javaGame.role.*;
import com.javaGame.role.enemy.Boss;
import com.javaGame.util.CreateEnemy;
import com.javaGame.util.ImageInit;
import com.javaGame.util.KeyPress;
import com.javaGame.util.MusicUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame implements Runnable {
    /**内存画布*/
    private Image memaryImage;
    /**内存画笔*/
    private Graphics memaryGraphics;
    /**地图*/
    private GameMap gameMap = new GameMap();
    /**女孩*/
    public static Girl girl = new Girl();
    /**boss*/
    private Boss boss = new Boss();
    /**游戏是否运行*/
    public static boolean isRun = true;
    /**角色集合*/
    public static List<BaseRole> roleList = new ArrayList<>();
    /**产生怪物对象*/
    private CreateEnemy createEnemey = new CreateEnemy();
    /**玩家得分*/
    public static int pointer;
    /**音乐播放*/
    private MusicUtil musicUtil = new MusicUtil(
            "music/background.mp3",true);
    /**女孩生命*/
    public static int girlNum = 3;
    /**通关是否成功*/
    public static boolean isPass;

    public MainFrame(){
        //设置窗体大小
        this.setSize(1000,700);
        //设置窗体可见
        this.setVisible(true);
        //窗体关闭程序结束
        this.setDefaultCloseOperation(3);
        //窗体居中显示
        this.setLocationRelativeTo(null);
        //窗体不能改变大小
        this.setResizable(false);

        //注册键盘事件监听
        this.addKeyListener(new KeyPress());

        //创建内存画布
        this.memaryImage = this.createImage(1000,700);
        // 得到内存画笔，内存画笔绘制的内容全在内存画布中
        this.memaryGraphics = this.memaryImage.getGraphics();
        //设置画笔颜色
        this.memaryGraphics.setColor(Color.PINK);
        //设置画笔字体
        this.memaryGraphics.setFont(new Font("黑体",Font.BOLD,30));

        //图片初始化
        ImageInit.init(this);

        roleList.add(girl);
        roleList.add(boss);
        roleList.add(new Prop(200,200,Prop.FIRE));

        //启动线程，线程启动后执行当前类的run()
        new Thread(this).start();
    }

    public static void main(String[] args) {
        new MainFrame();
    }

    @Override
    public void run() {
        while(isRun){
            this.drawFrame(this.getGraphics());

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //停止音乐
        musicUtil.stopMusic();
        //卸载当前窗体
        this.dispose();
        if(isPass == true){//通关成功
            new SucessFrame();
        }
        else{//通关失败
            new LoseFrame();
        }

    }

    /**
     * 绘制窗体
     * @param g  窗体画笔
     */
    private void drawFrame(Graphics g) {
        createEnemey.create();

        gameMap.drawMySelf(this.memaryGraphics);

        //绘制角色
        for(int i=0;i<roleList.size();i++){
            roleList.get(i).drawMySelf(this.memaryGraphics);
        }
        this.drawInfo();

        //将叠加后图片绘制在中窗体中
        g.drawImage(this.memaryImage,0,0,this);
    }

    /**
     * 绘制游戏信息
     */
    private void drawInfo(){
        this.memaryGraphics.drawString("玩家得分："+pointer
            ,50,80);
        this.memaryGraphics.drawString("炸弹："+girl.getBomNum()
                ,50,110);
        this.memaryGraphics.drawString("玩家生命："+girlNum
                ,50,140);
    }

}
